package edu.hawaii.duedates;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Creates an object that holds information about a given online
 * library system. Object holds the systems url, the login form
 * name (if one exists), as well as the login form parameter names.
 * 
 * @author Tyler Wolff and Scheller Sanchez
 */

public class LibraryVault {
  
  String libUrl;
  String formName;
  String param1;
  String param2;
 
  /**
   * Constructs the basic LibraryVault object.
   * 
   * @param libUrl The url of the library system.
   * @param formName The name of the form, if any.
   * @param param1 The first login form parameter.
   * @param param2 The second login form parameter.
   */
  
  public LibraryVault (String libUrl, String formName, String param1, String param2) {
    
    this.libUrl = libUrl;
    this.formName = formName;
    this.param1 = param1;
    this.param2 = param2;
    
  }
  
  /**
   * Attempts to login to a given Library system with user information. System makes use of an
   * HttpUnit WebConversation to scan a systems web page for login form, then inputs user
   * information and submits.
   * 
   * @param id The user ID or first login parameter.
   * @param pass The user password or second login parameter.
   * @return The WebConversation instance after successful login.
   * @throws Exception If login was not successful with these credentials.
   */
  public WebConversation login (String id, String pass) throws Exception {

    // If website has bogus JavaScript code, do not throw error.
    HttpUnitOptions.setExceptionsThrownOnScriptError(false);
    // create the conversation object which will maintain state for us
    WebConversation conversation = new WebConversation();
    WebRequest request = new GetMethodWebRequest(this.libUrl);
    WebResponse response = conversation.getResponse(request);
    
    // If site does not have a named login form
    if (this.formName == null) {
      WebLink link = response.getLinkWith("My Account");
      link.click();
      response = conversation.getCurrentPage();
      WebForm form = response.getForms()[0];
      form.setParameter(this.param1, id);
      form.setParameter(this.param2, pass);
      form.submit(); 
      response = conversation.getCurrentPage();
      
      // Return null conversation if Login unsuccessful
      if (response.getText().contains("Login failed")) {
        conversation = null;
        System.out.println("Login was unsuccessful. Please try again.");
      }
    }
    else {
      WebForm form = response.getFormWithName(this.formName);
      form.setParameter(this.param1, id);
      form.setParameter(this.param2, pass);
      form.submit();
      response = conversation.getCurrentPage();

      // Return null conversation if Login unsuccessful
      if (response.getText().contains("Sorry, Login unsuccessful")) {
        conversation = null;
        System.out.println("Login was unsuccessful. Please try again.");
      }
    }

    return conversation;

  }
  
}
